import Yaml from 'yaml';
import Path from "node:path";
import KoaBootFile from "@framework/web/KoaBootFile";

const YAML_FILE = "application.yml";

/**
 * 获取YML文件
 */
function getContent(): any {
    const koaBootFile = new KoaBootFile(KoaBootFile.getResourcePath(), YAML_FILE);
    const content = koaBootFile.readString();
    if (content) {
        return Yaml.parse(content);
    }
}

// 自动配置
const config: GlobalConfig = getContent();

/**
 * 全局自动配置
 */
class GlobalAutoConfigurable {

    /**
     * 服务配置
     * @private
     */
    public static readonly server: FrameworkServer = {
        port: config.server.port || 8080
    }

    /**
     * KoaBoot配置
     */
    public static readonly koaBoot: KoaBoot = {
        prefix: config.koaBoot.prefix || "/",
        redis: {
            enable: config.koaBoot.redis?.enable ? true : false,
            host: config.koaBoot.redis?.host || "127.0.0.1",
            port: config.koaBoot.redis?.port || 6379,
            username: config.koaBoot.redis?.username || "",
            password: config.koaBoot.redis?.password || "",
            database: config.koaBoot.redis?.database || 0
        },
        mysql: {
            enable: config.koaBoot.mysql?.enable ? true : false,
            database: config.koaBoot.mysql?.database || "koa-boot",
            username: config.koaBoot.mysql?.username || "root",
            password: config.koaBoot.mysql?.password || "123456",
            host: config.koaBoot.mysql?.host || "127.0.0.1",
            port: config.koaBoot.mysql?.port || 3306,
            modelDir: config.koaBoot.mysql?.modelDir || "model",
            default: {
                charset: config.koaBoot.mysql?.default?.charset || "utf8",
                timestamps: config.koaBoot.mysql?.default?.timestamps || false,
                createdAt: config.koaBoot.mysql?.default?.createdAt || "create_time",
                updatedAt: config.koaBoot.mysql?.default?.updatedAt || "update_time",
                deletedAt: config.koaBoot.mysql?.default?.deletedAt || "is_deleted"
            },
            pool: {
                max: config.koaBoot.mysql?.pool?.max || 10,
                min: config.koaBoot.mysql?.pool?.min || 0,
                acquire: config.koaBoot.mysql?.pool?.acquire || 30000,
                idle: config.koaBoot.mysql?.pool?.idle || 10000
            }
        }
    }

    /**
     * 日志配置
     * @private
     */
    public static readonly logger: KoaLogger = {
        type: config.logger.type || "dateFile",
        filePath: config.logger.filePath || "./logs",
        filename: Path.join(KoaBootFile.getRootPath(), config.logger.filePath || './logs', config.logger.filename || "access"),
        pattern: config.logger.pattern || "-yyyy-MM-dd.log",
        root: config.logger.root || "DEBUG",
        daysToKeep: config.logger.daysToKeep || 7,
        compress: config.logger.compress || false,
        alwaysIncludePattern: config.logger.alwaysIncludePattern || true,
        layout: config.logger.layout || { type: 'pattern', pattern: '[%d{yyyy-MM-dd hh:mm:ss}] [%p] %m' }
    }
}

export default GlobalAutoConfigurable;